//
//  SlowBlood2Layer.m
//  SpaceStorm
//
//  Created by  mmj on 13-8-7.
//
//

#import "SlowBlood2Layer.h"

@implementation SlowBlood2Layer
@synthesize width = _width;
@synthesize height = _height;
/**
 返回一个两层缓动血条的实例
 @param bgFrameName 背景图片的FrameName
 @param bloodFrameName 血条图片的FrameName
 @returns 返回实例
 */
+(id) bloodWithBG:(NSString*)bgFrameName
            blood:(NSString*)bloodFrameName
{
    return [[[self alloc] initWithBG:bgFrameName  blood:bloodFrameName] autorelease];
}

/**
 初始化一个两层缓动血条的实例
 @param bgFrameName 背景图片的FrameName
 @param slowFrameName 缓动图片的FrameName
 @param bloodFrameName 血条图片的FrameName
 @returns 返回实例
 */
-(id) initWithBG:(NSString*)bgFrameName
           blood:(NSString*)bloodFrameName

{
    if( (self=[super init]))
    {
        //血槽的背景
        _bgSprite = [CCSprite spriteWithSpriteFrameName:bgFrameName];
        [self addChild:_bgSprite];
        self.width =  _bgSprite.contentSize.width;
        self.height =  _bgSprite.contentSize.height;
        
        
        //血条
        CCSprite *bloodSprite = [CCSprite spriteWithSpriteFrameName:bloodFrameName];
        _bloodTimer = [CCProgressTimer progressWithTexture:[self sprite2Texture2D:bloodSprite]];
        _bloodTimer.type = kCCProgressTimerTypeHorizontalBarLR;
        _bloodTimer.percentage = 100;
        _bloodTimer.position = ccp(0, 0);
        [self addChild:_bloodTimer];
        
        
    }
    
    return self;
}

/**
 设置血条的进度，0～100之间
 @param progress 进度，0～100之间
 */
-(void) setProgress:(int)progress
{
    if(progress < 0 || progress > 100) return;
    
    //改变血条
    _bloodTimer.percentage = progress;
    
}

/**
 将一个sprite转换成Texture2D
 @param sprite
 @returns
 */
-(CCTexture2D*) sprite2Texture2D:(CCSprite*)sprite
{
    CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
    sprite.anchorPoint = ccp(0, 0);
    sprite.position = ccp(0, 0);
    sprite.flipY = YES;
    [renderTexture addChild:sprite];
    [renderTexture begin];
    [sprite draw]; // or [spr visit];
    [renderTexture end];
    
    CCTexture2D * result = renderTexture.sprite.texture;
    return result;
}

-(void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}


@end
